#include "TextQuery.h"

using namespace std;
TextQuery::TextQuery(ifstream& is) :text_data(new vector<string>)
{
	string text;
	while (getline(is, text))
	{
		text_data->push_back(text);
		int n = text_data->size() - 1;
		istringstream line(text);
		string word;
		while (line>>word)
		{
			auto& lines = wm[word];//map go to page387
			if (!lines)
			{
				lines.reset(new set<line_no>);
			}
			lines->insert(n);
		}
	}
}

QueryResult TextQuery::analyze(const string& sought) const
{
	static shared_ptr<set<line_no>> nodata(new set<line_no>);
	auto locat = wm.find(sought);
	if (locat==wm.end())
	{
		return QueryResult(sought, nodata, text_data);
	}
	else
	{
		return QueryResult(sought, locat->second, text_data);
	}
}

void QueryResult::myprint(ostream& os, const QueryResult& result) const
{ 

}